<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<title>江苏绿地会  积分兑换</title>
		<link href="{$Think.config.static_path}duihuan/css/main.css" rel="stylesheet" type="text/css">
		<style type="text/css">
			.hide{
				display: none;
			}
			#loading{
				text-align:center;
				position:fixed;
				top:45%;
				height:100%;
				left:50%;
			}
			.iframe-div {
				background: rgba(0,0,0,0.7);
				height: 100%;
				width: 100%;
				position: fixed;
				top: 0;
			}

			.iframe-content {
				background: #CA403D;
				width: 80%;
				margin: 100px 5%;
				padding: 5%;
				color: #FFDA5E;
			}

			.title {
				text-align: center;
				font-size: 14px;
				line-height: 24px;
			}

			.sixcode {
				margin-top: 13px;
			}

			.sixcode input {
				border: 0;
				border-radius: 5px;
				width: 90%;
				padding: 5px 5%;
				height: 30px;
				background: #eee;
			}

			.two-button {
				margin-top: 15px;
				text-align: center;
			}

			.two-button button {
				border: 0;
				width: 100px;
				height: 36px;
				border-radius: 5px;
				background: #D2D1D1;
				color: #373737;
				font-size: 16px;
				margin: 0 10px;
			}

			.two-button button.submit {
				background: #FFDA5E;
				color: #AF322E;
			}
		</style>
	</head>
	<body class="bg layout">
		<div class="bg-white">
			<div class="content">
				<div class="iframe-div hide" >
					<div class="iframe-content">

						<div class="title">
							<p>恭喜新业主入会！</p>
							<p class="exchangeinfo"></p>
							<p>4位授权码详询置业顾问。</p>
						</div>
						<div class="sixcode">
							<p> 
								<input type="text" maxlength="6" id="code" placeholder="请输入4位授权码">
							</p>
						</div>
						<div class="two-button">
							<button class="cancel">取消</button>
							<button class="submit">确定</button>
						</div>
					</div>
				</div>
				<empty name="isexhange">
				<div class="address">
					<h3>请选择您要兑换商品的地址</h3>
					<select id="city"  onChange="change('')">
						<option value="0">--请选择--</option>
                        <volist name="list" id="val">
							<option value="{$val.id}">{$val.title}</option>
                        </volist>
		
					</select>
					<p>仅可在选取地点领取兑换商品。</p>
					<!-- <h3>您要兑换商品的地址</h3>
					<h3 style="padding:10px 0">徐州</h3>
					<p>仅可在选取地点领取兑换商品。</p> -->
				</div>
                <else/>
              		<div class="address"> <h3>您要兑换商品的地址</h3><h3 style="padding:10px 0">{$storeName}</h3><p>仅可在选取地点领取兑换商品。</p></div>
                </empty>
				<div class="line1">
					<div class="border"></div>
					<div class="">
						<div class="ruleimg">
							<img src="{$Think.config.static_path}/duihuan/images/icon1.png" class="icon2">
						</div>
						<div class="prize">
							<div>
								<table>
									<thead>
										<th>商品</th>
										<th>市场价</th>
										<th>会员兑换价</th>
										<th>兑换</th>
									</thead>
									<tbody id="list">
									<empty name="isexhange">
                                    <volist name="data" id="val">
										<tr>
											<td>{$val.title}</td>
											<td>{$val.price}元</td>
											<td><span class="grade">{$val.score}</span>积分</td>
											<td>
                                            	<if condition="$val['num'] egt $val['total']">
                                                	<span class="exchange gray"  >已兑完</span>
                                                <else/>
													<span class="exchange" onClick="exchange(this,'{$val.sid}','{$val.id}','{$val.title}',0)" >我要兑换</span>
                                                </if>
											</td>
										</tr>
                                        </volist>
									</empty>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	<input type="hidden" id="htmlsid"/>
	<input type="hidden" id="htmlpid"/>
	<input type="hidden" id="htmltitle"/>
	</body>
    <include file="public:share" />
	<script type="text/javascript" src="{$Think.config.static_path}/jquery-2.1.4.min.js"></script>
	<script type="text/javascript" src="{$Think.config.static_path}duihuan/js/util.js"></script>
	<script type="text/javascript">
		$(function(){
			if("{$isexhange}"){
				change("{$sid}");
			}
		})
		
		var myscore = "{$myscore}";
		function change(sid) {
			var sid = sid ? sid : $("#city  option:selected").val() == 0 ? 1  : $("#city  option:selected").val();
			var html = '',
			str = '';
			Util.ajax({
				method: 'post',
				data: 'sid=' + sid+'&openid={$openid}',
				url: '__URL__/ajaxProduct'
			},
			function(json) {
				if (json.status > 0) {
					var len = json.data.length;
					$.each(json.data,function(i, val) {
						//alert(json.data[i]['title']);
						//alert(typeof parseInt(val.num));
						if(parseInt(val.num) >= parseInt(val.total) && val.code=='我要兑换'){
							str = '<span class="exchange gray"  >已兑完</span>';
						}else{
							if (val.isexchange == 0 ) {
								if(myscore >= 1 || val.sid ==1 || val.sid == 5){
									str = '<span class="exchange" id=p'+val.id+' onClick=exchange(this,"' + val.sid + '","' + val.id + '","' + val.title + '",0) >我要兑换</span>';
								}else{
									str = '<span class="exchange" id=p'+val.id+' onClick=exchange(this,"' + val.sid + '","' + val.id + '","' + val.title + '",1) >授权码</span>';
								}
								
							} else {
								if(val.code!='我要兑换'){
									str = '<span class="exchange">' + val.code + '</span>';
								}else{
									str = '<span class="exchange gray">' + val.code + '</span>';
								}
							}
						}
							
						html += '<tr><td>' + val.title + '</td><td>' + val.price + '元</td><td><span class="grade">' + val.score + '</span>积分</td><td>' + str + ' </td></tr>';
					})
				} else {
					html = '<tr><td colspan=4>' + json.data + '</td></tr>'
				}
				$("#list").html(html);
			});
			
			
		}
		var  tmp = true;
		$(".cancel").on('click', function(){
			$(".iframe-div").addClass('hide');
			tmp =true;
		})

		//兑奖码核对显示浮层
		function checkcode(sid,pid,title){
			tmp =false;
			$(".iframe-div").removeClass('hide');
			$("#code").val("");
			$(".exchangeinfo").html("新主会员专享“授权码”即刻兑换"+title+"！");
			$("#htmlsid").val(sid);
			$("#htmlpid").val(pid);
			$("#htmltitle").val(title);
		}
		//兑奖码核对处理
		$(".submit").click(function(){
			var code_id = $("#code").val();
			var sid = $("#htmlsid").val();
			var title = $("#htmltitle").val();
			if(code_id == ""){
				Util.info("请输入4位授权码",16,3000);
				return;
			}else{
				$.getJSON('__URL__/checkCode', {sid:sid,code_id:code_id},function(json) {
					if(json.status > 0){
						$(".iframe-div").addClass('hide');
						duihuan(1,$("#htmlpid").val(),title,code_id,sid);
					}else{
						Util.info(json.info,16,3000);
					}
				})
			}
		})
	
		/**
		*处理兑换数据
		*/
		function duihuan(ispay,pid,title,code_id,sid) {
			var cityName = $("#city  option:selected").text();
			tmp =false;
			Util.ajax({
				method: 'post',
				data: 'sid=' + sid+ '&pid=' + pid + '&title=' + title + '&openid={$openid}&ispay='+ispay+'&code_id='+code_id,
				url: '__URL__/exchange'
			},
			function(json) {
				if (json.status > 0) {
					$(".exchange").addClass("gray");
					if(json.key == ""){
						$.getJSON("__URL__/getOrderInfo",{openid:"{$openid}"},function(json){
							$("#p"+json.pid).html(json.key);
							$("#p"+json.pid).removeClass('gray');
						})
					}else{
						$("#p"+json.pid).html(json.key);
						$("#p"+json.pid).removeClass('gray');
					}
					var html = '<h3>您要兑换商品的地址</h3><h3 style="padding:10px 0">' + cityName + '</h3><p>仅可在选取地点领取兑换商品。</p>';
					$(".address").html(html);
				} else {
					Util.info(json.info);
					tmp=true;
				}
			})
			
		}
		//执行兑换函数
		function exchange(obj, sid, pid, title,op) {
			if (!$(obj).hasClass("gray")) {
				var city = $("#city  option:selected").val();
				
				if (parseInt(city) == 0) {
					Util.info("请选择您要兑换商品的地址");
					return;
				}
				var grade = $(obj).parents("tr").find(".grade").text();
				if(tmp){
					if(op == 1){
						console.log(pid);
						checkcode(sid,pid,title);tmp =false;
					}else{
						if(myscore >= grade){
							Util.showmsg("您确定兑换么，兑换消耗" + grade + "积分",function gg(){duihuan(0,pid,title,null,sid);},function tt(){tmp=true;}, "confirm");tmp =false;
						}else{
							Util.info("积分不足~");
						}
					}
				}
			}
		}
	</script>
</html>